package com.moxiao.yfjh.module.blind.service.moment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMemberMomentUpdateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.BlindMementStatisticsVo;
import com.moxiao.yfjh.module.blind.convert.moment.BlindMemberMomentConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.moment.BlindMemberMomentDO;
import com.moxiao.yfjh.module.blind.dal.mysql.moment.BlindMemberMomentMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.moment.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员动态 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class BlindMemberMomentServiceImpl implements BlindMemberMomentService {

    @Resource
    private BlindMemberMomentMapper memberMomentMapper;

    @Override
    public Long createMemberMoment(BlindMemberMomentCreateReqVO createReqVO) {
        // 插入
        BlindMemberMomentDO memberMoment = BlindMemberMomentConvert.INSTANCE.convert(createReqVO);
        memberMomentMapper.insert(memberMoment);
        // 返回
        return memberMoment.getId();
    }

    @Override
    public void updateMemberMoment(BlindMemberMomentUpdateReqVO updateReqVO) {
        // 校验存在
        validateMemberMomentExists(updateReqVO.getId());
        // 更新
        BlindMemberMomentDO updateObj = BlindMemberMomentConvert.INSTANCE.convert(updateReqVO);
        memberMomentMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberMoment(Long id) {
        // 校验存在
        validateMemberMomentExists(id);
        // 删除
        memberMomentMapper.deleteById(id);
    }

    private void validateMemberMomentExists(Long id) {
        if (memberMomentMapper.selectById(id) == null) {
            throw exception(MEMBER_MOMENT_NOT_EXISTS);
        }
    }

    @Override
    public BlindMemberMomentDO getMemberMoment(Long id) {
        return memberMomentMapper.selectById(id);
    }

    @Override
    public List<BlindMemberMomentDO> getMemberMomentList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberMomentMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlindMemberMomentDO> getMemberMomentPage(BlindMemberMomentPageReqVO pageReqVO) {
        return memberMomentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlindMemberMomentDO> getMemberMomentList(BlindMemberMomentExportReqVO exportReqVO) {
        return memberMomentMapper.selectList(exportReqVO);
    }

    @Override
    public BlindMemberMomentDO getMemberMomentTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<BlindMemberMomentDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(BlindMemberMomentDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(BlindMemberMomentDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return memberMomentMapper.selectOne(lqw);
    }

    @Override
    public Integer getBlindMemberMomentNo(String momentNo) {
        return memberMomentMapper.getMemberMomentNo(momentNo);
    }

    @Override
    public BlindMementStatisticsVo getMemberMomentStatistics(Long userId) {
        BlindMementStatisticsVo blindMementStatisticsVo = new BlindMementStatisticsVo();
        blindMementStatisticsVo.setTotal_num(memberMomentMapper.getMomentUer(userId));
        //0 文字 1 照片 2 动态
        blindMementStatisticsVo.setText_num(memberMomentMapper.getMomentTypeCount(0,userId));
        blindMementStatisticsVo.setImg_num(memberMomentMapper.getMomentTypeCount(1,userId));
        blindMementStatisticsVo.setVideo_num(memberMomentMapper.getMomentTypeCount(2,userId));
        return blindMementStatisticsVo;
    }

    @Override
    public Long getBlindMemberMomentCount(Long userId) {
        return memberMomentMapper.selectCount(BlindMemberMomentDO::getUserId,userId);
    }


}
